@echo off
title TimerPOS Management System

:: 设置窗口颜色
color 0A

echo ======================================
echo    TimerPOS Management System
echo ======================================
echo.

:: 获取脚本所在目录
set ROOT_DIR=%~dp0
echo [%date% %time%] Working directory: %ROOT_DIR%
:: 检查后端环境
cd "%ROOT_DIR%frontend"
if exist dist rmdir /s /q dist
:: check node_modules
if not exist "node_modules" (
    echo [%date% %time%] not found node_modules 
    echo [%date% %time%] run  npm install 
    call npm install 
) 
call npm run build
cd "%ROOT_DIR%"
:: Copy frontend dist to backend/web
echo [%date% %time%] Copying frontend/dist to backend/web...
if exist backend\web rmdir /s /q backend\web
if not exist backend\web mkdir backend\web
xcopy /Y /E /I frontend\dist backend\web\

:: 检查后端环境
cd "%ROOT_DIR%backend"
if not exist ".venv" (
    echo [%date% %time%] Creating backend virtual environment...
    python -m venv .venv
    call .venv\Scripts\activate
    pip install -r requirements.txt
) else (
    echo [%date% %time%] Backend virtual environment exists
)

:: 启动后端服务器
echo [%date% %time%] Starting backend server...
start cmd /k "cd /d "%ROOT_DIR%backend" && call .venv\Scripts\activate && uvicorn main:app --reload --host 0.0.0.0 --port 8000 --workers 2"

:: 等待前端启动
timeout /t 2

:: 启动浏览器
echo [%date% %time%] Opening browser...
start http://localhost:8000

echo.
echo [%date% %time%] System started successfully!